<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/2
 * Time: 16:58
 * 供应商管理
 */
class project extends MY_Controller
{

    public function __construct()
    {

        parent::__construct();
        $this->load->model('project_model');

    }

    //供应商管理
    public function index()
    {
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',
                'arg'=> $this->input->get('search'),
                'side' => 'both'
            ];

            $data = $this->project_model->search('*',0,$searchArg,$page_num,$start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
            $this->load->view('admin/project/list',$viewData);
        } else {
            $data = $this->project_model->page('project',0,$page_num,$start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条

            ];

            $this->load->view('admin/project/list',$viewData);
        }
    }

    //新增功能
    public function add(){
        $data = [
            'code' => $this->input->post('code'),
            'name' => $this->input->post('name'),
            'source' => $this->input->post('source'),
            'linkman' => $this->input->post('linkMan'),
            'note' => $this->input->post('note'),
            'creater' => $_COOKIE['username'],
            'createTime' => time()
        ];
        //是否会有添加失败的情况?

        $resultData =  $this->project_model->add($data);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/project/index');
        }
    }

    //新增页面
    public function addView(){

        $this->load->view('/admin/project/add');
    }

    //编辑功能
    public function edit(){
        //用户id
//        print_r($_POST);
//        die();
        $where = [
            'id' => $this->input->post('id')
        ];
        //todo 更多字段
        $data = [
            'name' => $this->input->post('name'),
            'source' => $this->input->post('simple'),
            'linkman' => $this->input->post('linkMan'),
            'note' => $this->input->post('note'),
        ];

//        print_r($data);
//        die();

        //是否会有添加失败的情况?
        $resultData =  $this->project_model->edit($data,$where);

//        print_r($resultData);
//        die();

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/project/index');
        }

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $data['list'] = $this->project_model->getByFieldQueryData('*',$arg,0);

//        print_r($data);
//        die();

        $this->load->view('/admin/project/edit',$data);
    }

    //删除功能
    public function remove(){
        $data = [
            'id' => $this->input->get('id'),
        ];

        //是否会有添加失败的情况?
        $resultData =  $this->project_model->remove($data);
        if($resultData){
            echo '<script>alert("删除成功")</script>';
            redirect('/admin/project/index');
        }
    }
    
    


}